<?php

namespace common\models\user;

use Yii;

/**
 * This is the model class for table "{{%user_role}}".
 *
 * @property int $id
 * @property string $name 角色名称
 * @property int $system 系统
 * @property int $status 状态
 * @property int $created_at 提交时间
 * @property int $updated_at 更新时间
 * @property int $deleted_at 删除时间
 */
class UserRole extends \common\db\ActiveRecord {

    const STATUS_DEFAULT = 0;
    const STATUS_ACTIVE = 10;
    //
    const ADMINER = 2;
    const ROLE_REPORTER = 6;
    const ROLE_EDITOR = 5;
    const ROLE_CHIEF_EDITOR = 4;
    const ROLE_MASTER = 3;

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%user_role}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['system', 'status', 'created_at', 'updated_at', 'deleted_at'], 'integer'],
            [['name'], 'string', 'max' => 32],
            [['name'], 'unique'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'name' => '角色名称',
            'system' => '系统',
            'status' => '状态',
            'created_at' => '提交时间',
            'updated_at' => '更新时间',
            'deleted_at' => '删除时间',
        ];
    }

    /**
     * 查询系统应用角色
     */
    public static function findSystem() {
        return static::find()->active(static::STATUS_ACTIVE)->andWhere([
            'system' => 1
        ])->all();
    }

    /**
     * 查询系统应用角色
     */
    public static function findWork() {
        return static::find()->active(static::STATUS_ACTIVE)->andWhere([
            'system' => 0
        ])->all();
    }

    public static function findAdminer() {
        return static::find()->active(static::STATUS_ACTIVE)->andWhere([
            'system' => 1,
        ])->andWhere([
            '!=', 'id', 1,
        ])->one();
    }

}
